package com.study.d012Components.media;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

/**
*@description
*@author xtayhame
*date 2020年3月18日
*version
*since v1.0
*/
public class MediaViewMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {


		AnchorPane anchorPane = new AnchorPane();

		HBox hBox = new HBox(10);
		Button playbut = new Button("播放");
		Button cancelbut = new Button("暂停");

		hBox.getChildren().addAll(playbut,cancelbut);

		Media media = new Media("file:/H:/MV/花田错-王力宏.mp4");
		MediaPlayer player = new MediaPlayer(media);
		MediaView view = new MediaView(player);
		view.setFitHeight(350);

		anchorPane.getChildren().addAll(hBox,view);
		AnchorPane.setTopAnchor(hBox, 10.0);
		AnchorPane.setLeftAnchor(hBox, 20.0);

		AnchorPane.setTopAnchor(view, 30.0);
		AnchorPane.setLeftAnchor(view, 10.0);

		stage.setScene(new Scene(anchorPane, 500, 400));
		stage.show();

		//绑定宽和高
		view.fitHeightProperty().bind(stage.heightProperty().subtract(50));
		view.fitWidthProperty().bind(stage.widthProperty().subtract(50));

		playbut.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				player.play();
			}
		});

		cancelbut.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				player.stop();
			}
		});
	}
}
